package com.imnc.eatmap.ui;

import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.view.MenuItem;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.imnc.eatmap.R;
import com.imnc.eatmap.model.PoiInfoParcelable;
import com.imnc.eatmap.ui.adapter.SearchResultAdapter;
import com.imnc.eatmap.ui.base.BaseActivity;

public class SearchResultActivity extends BaseActivity {

	private PullToRefreshListView mResultListView = null;
	private List<PoiInfoParcelable> allPoiResult = null;
	private SearchResultAdapter mAdapter = null;

	private ActionBar mActionBar = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search_result);
		findView();
		initData();
	}

	private void findView() {
		mResultListView = (PullToRefreshListView) findViewById(R.id.plv_search_result);
		mActionBar = getSupportActionBar();
	}

	private void initData() {
		Bundle bundle = getIntent().getExtras();
		allPoiResult = bundle
				.getParcelableArrayList(MainActivity.MAP_ALL_POI_RESULT);

		if (allPoiResult == null) {
			defaultFinish();
		}
		mAdapter = new SearchResultAdapter(this);
		mResultListView.setMode(Mode.DISABLED);
		mResultListView.setAdapter(mAdapter);

		if (allPoiResult != null && allPoiResult.size() != 0) {
			mAdapter.setItemContent(allPoiResult);
			mAdapter.notifyDataSetChanged();
		}

		mActionBar.setDisplayHomeAsUpEnabled(true);

		mResultListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parnet, View view,
					int position, long id) {

				Bundle bundle = new Bundle();
				bundle.putParcelable("poiInfoParcelable",
						allPoiResult.get(position));
				startActivity(PoiResultDetailActivity.class, bundle);

			}
		});
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			this.finish();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
}
